package pvz.a;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;

import org.standard.Utility;

public class PlantCardImage extends PVZImage {
	private String name;
	private long refreshtime;
	private PlantImage plant;
	private long lastactivetime = 0L;
	private Boolean active = true;

	public PlantCardImage(String name, long refreshtime) {
		this.name = name;
		this.refreshtime = refreshtime;
		String fileName = "pvz/images/Card/Plants/" + name + ".png";
		this.setImage(Utility.loadImage(fileName));
		this.setWidth(this.getImage().getWidth(null));
		this.setHeight(this.getImage().getHeight(null));
		setPlant(new PlantImage(name));
	}

	public PlantCardImage(String name, long refreshtime, int x, int y) {
		this.name = name;
		this.refreshtime = refreshtime;
		String fileName = "pvz/images/Card/Plants/" + name + ".png";
		this.setImage(Utility.loadImage(fileName));
		this.setWidth(this.getImage().getWidth(null));
		this.setHeight(this.getImage().getHeight(null));
		this.setPosX(x);
		this.setPosY(y);
	}

	public void reset(long time) {
		this.lastactivetime = time;
		setActive(true);
	}

	public Image getImageByTime(long time) {
		if (getActive()) {
			if ((time - lastactivetime) > refreshtime) {
				setActive(false);
				lastactivetime = time;
			}
			time = (time - lastactivetime) % refreshtime;
		} else {
			time = 1;
		}
		Image buffImage = Utility.createImage(this.getImage().getWidth(null),
				this.getImage().getHeight(null), true);
		Graphics2D g = (Graphics2D) buffImage.getGraphics();
		g.drawImage(this.getImage(), 0, 0, null);
		AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
				0.5f);
		g.setColor(Color.yellow);
		g.setComposite(ac);
		g.fillRect(0,
				this.getImage().getHeight(null)
						- (int) (this.getImage().getHeight(null)
								* (refreshtime - time) / refreshtime), this
						.getImage().getWidth(null), (int) (this.getImage()
						.getHeight(null)
						* (refreshtime - time) / refreshtime));
		return buffImage;
	}

	public void setPlant(PlantImage plant) {
		this.plant = plant;
	}

	public PlantImage getPlant() {
		return plant;
	}

	public void setActive(Boolean active) {
		this.active = active;
	}

	public Boolean getActive() {
		return active;
	}
}
